00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _expression_hpp_
00022 #define _expression_hpp_
00023
00024 #include <iostream>
00025 #include <boost/shared_ptr.hpp>
00026 #include <boost/format.hpp>
00027 #include <boost/assert.hpp>
00028 #include <boost/static_assert.hpp>
00029 #include <boost/type_traits/is_same.hpp>
00030 #include <boost/serialization/base_object.hpp>
00031
00032 #include <boost/serialization/export.hpp>
00033
00034 #include <gridpack/expression/variable.hpp>
00035
00036 namespace gridpack {
00037 namespace optimization {
00038
00039
00040
00041 template <typename T> class ConstantExpression;
00042 typedef ConstantExpression<int> IntegerConstant;
00043 typedef ConstantExpression<double> RealConstant;
00044
00045 class VariableExpression;
00046
00047 class UnaryExpression;
00048 class UnaryMinus;
00049 class UnaryPlus;
00050
00051 class BinaryExpression;
00052 class Multiplication;
00053 class Division;
00054 class Addition;
00055 class Subtraction;
00056 class Exponentiation;
00057
00058 class Constraint;
00059 class LessThan;
00060 class LessThanOrEqual;
00061 class GreaterThan;
00062 class GreaterThanOrEqual;
00063 class Equal;
00064
00065 class Function;
00066
00067
00068
00069
00070
00071 class ExpressionVisitor
00072 : private utility::Uncopyable
00073 {
00074 public:
00075
00076
00077 ExpressionVisitor(void);
00078
00079
00080 ~ExpressionVisitor(void);
00081
00082 virtual void visit(IntegerConstant& e);
00083 virtual void visit(RealConstant& e);
00084 virtual void visit(VariableExpression& e);
00085
00086 virtual void visit(UnaryExpression& e);
00087 virtual void visit(UnaryMinus& e);
00088 virtual void visit(UnaryPlus& e);
00089
00090 virtual void visit(BinaryExpression& e);
00091 virtual void visit(Multiplication& e);
00092 virtual void visit(Division& e);
00093 virtual void visit(Addition& e);
00094 virtual void visit(Subtraction& e);
00095 virtual void visit(Exponentiation& e);
00096
00097 virtual void visit(Constraint& e);
00098 virtual void visit(LessThan& e);
00099 virtual void visit(LessThanOrEqual& e);
00100 virtual void visit(GreaterThan& e);
00101 virtual void visit(GreaterThanOrEqual& e);
00102 virtual void visit(Equal& e);
00103
00104 virtual void visit(Function& e);
00105
00106 };
00107
00108
00109
00110
00111 class Expression {
00112 public:
00113
00114
00115 Expression(int prec)
00116 : p_precedence(prec)
00117 {}
00118
00119
00120 Expression(const Expression& old)
00121 : p_precedence(old.p_precedence)
00122 {}
00123
00124
00125 virtual ~Expression(void) {}
00126
00127
00128 int precedence(void) const
00129 {
00130 return p_precedence;
00131 }
00132
00133
00134 virtual bool null(void) const
00135 {
00136 return this->p_null();
00137 }
00138
00139
00140 void evaluate(void) const
00141 {
00142 std::cout << this->p_render() << std::endl;
00143 }
00144
00145
00146 std::string render(void) const
00147 {
00148 return this->p_render();
00149 }
00150
00151
00152 void accept(ExpressionVisitor& visitor)
00153 {
00154 this->p_accept(visitor);
00155 }
00156
00157 protected:
00158
00159
00160 const int p_precedence;
00161
00162
00163 virtual std::string p_render(void) const = 0;
00164
00165 virtual void p_accept(ExpressionVisitor& e) = 0;
00166
00167
00168 virtual bool p_null(void) const
00169 {
00170 return true;
00171 }
00172
00173
00174 Expression(void) : p_precedence() {}
00175
00176 private:
00177
00178 friend class boost::serialization::access;
00179
00180 template<class Archive>
00181 void serialize(Archive &ar, const unsigned int)
00182 {
00183 ar & const_cast<int&>(p_precedence);
00184 }
00185 };
00186
00187 typedef boost::shared_ptr<Expression> ExpressionPtr;
00188
00189
00190
00191
00192 template <typename T>
00193 class ConstantExpression
00194 : public Expression
00195 {
00196 private:
00197
00198
00199 BOOST_STATIC_ASSERT((boost::is_same<T, double>::value || boost::is_same<T, int>::value));
00200
00201 public:
00202
00203
00204 ConstantExpression(const T& value)
00205 : Expression(0), p_value(value)
00206 {}
00207
00208
00209 ConstantExpression(const ConstantExpression& old)
00210 : Expression(old), p_value(old.p_value)
00211 {}
00212
00213
00214 ~ConstantExpression(void)
00215 {}
00216
00217
00218 T value(void) const
00219 {
00220 return p_value;
00221 }
00222
00223 protected:
00224
00225 const T p_value;
00226
00227 std::string p_render(void) const;
00228
00229 void p_accept(ExpressionVisitor& e)
00230 {
00231 e.visit(*this);
00232 }
00233
00234 bool p_null(void) const
00235 {
00236 return false;
00237 }
00238
00239
00240 ConstantExpression(void)
00241 : Expression(), p_value()
00242 {}
00243
00244 private:
00245
00246 friend class boost::serialization::access;
00247
00248 template<class Archive>
00249 void serialize(Archive &ar, const unsigned int)
00250 {
00251 ar & boost::serialization::base_object<Expression>(*this);
00252 ar & const_cast<T&>(p_value);
00253 }
00254 };
00255
00256
00257
00258
00259 class VariableExpression
00260 : public Expression
00261 {
00262 public:
00263
00264
00265 VariableExpression(VariablePtr v)
00266 : Expression(0), p_var(v)
00267 {}
00268
00269
00270 VariableExpression(const VariableExpression& old)
00271 : Expression(old), p_var(old.p_var)
00272 {}
00273
00274
00275 ~VariableExpression(void)
00276 {}
00277
00278
00279 std::string name(void) const
00280 {
00281 return p_var->name();
00282 }
00283
00284
00285 VariablePtr var(void) const
00286 {
00287 return p_var;
00288 }
00289
00290
00291 void var(VariablePtr v)
00292 {
00293 p_var = v;
00294 }
00295
00296 protected:
00297
00298
00299 VariablePtr p_var;
00300
00301 std::string p_render(void) const
00302 {
00303 return p_var->name();
00304 }
00305
00306 void p_accept(ExpressionVisitor& e)
00307 {
00308 e.visit(*this);
00309 }
00310
00311 bool p_null(void) const
00312 {
00313 return !static_cast<bool>(p_var);
00314 }
00315
00316
00317 VariableExpression(void)
00318 : Expression(), p_var()
00319 {}
00320
00321 private:
00322
00323 friend class boost::serialization::access;
00324
00325 template<class Archive>
00326 void serialize(Archive &ar, const unsigned int)
00327 {
00328 ar & boost::serialization::base_object<Expression>(*this);
00329 ar & p_var;
00330 }
00331 };
00332
00333
00334
00335
00336 class UnaryExpression
00337 : public Expression
00338 {
00339 public:
00340
00341
00342 UnaryExpression(const int& prec, const std::string& op,
00343 ExpressionPtr expr)
00344 : Expression(prec), p_operator(op), p_expr(expr)
00345 {}
00346
00347
00348 ~UnaryExpression(void)
00349 {}
00350
00351
00352 ExpressionPtr rhs()
00353 {
00354 return p_expr;
00355 }
00356
00357 protected:
00358
00359
00360 const std::string p_operator;
00361
00362
00363 ExpressionPtr p_expr;
00364
00365 std::string p_render(void) const
00366 {
00367 std::string s(this->p_operator);
00368 if (p_expr->precedence() > this->precedence()) {
00369 s += "[" + p_expr->render() + "]";
00370 } else {
00371 s += p_expr->render();
00372 }
00373 return s;
00374 }
00375
00376 void p_accept(ExpressionVisitor& e)
00377 {
00378 e.visit(*this);
00379 }
00380
00381 bool p_null(void) const
00382 {
00383 bool result(true);
00384 if (p_expr)
00385 result = p_operator.size() == 0 || p_expr->null();
00386 return result;
00387 }
00388
00389
00390 UnaryExpression(void)
00391 : Expression(0)
00392 {}
00393
00394 private:
00395
00396 friend class boost::serialization::access;
00397
00398 template<class Archive>
00399 void serialize(Archive &ar, const unsigned int)
00400 {
00401 ar & boost::serialization::base_object<Expression>(*this);
00402 ar & const_cast<std::string&>(p_operator) & p_expr;
00403 }
00404 };
00405
00406
00407
00408
00409
00410 class UnaryMinus
00411 : public UnaryExpression
00412 {
00413 public:
00414
00415
00416 UnaryMinus(ExpressionPtr expr)
00417 : UnaryExpression(3, "-", expr)
00418 {}
00419
00420
00421 UnaryMinus(const UnaryMinus& old)
00422 : UnaryExpression(old)
00423 {}
00424
00425
00426 ~UnaryMinus(void) {}
00427
00428 protected:
00429
00430 void p_accept(ExpressionVisitor& e)
00431 {
00432 e.visit(*this);
00433 }
00434
00435
00436 UnaryMinus(void)
00437 : UnaryExpression()
00438 {}
00439
00440 private:
00441
00442 friend class boost::serialization::access;
00443
00444 template<class Archive>
00445 void serialize(Archive &ar, const unsigned int)
00446 {
00447 ar & boost::serialization::base_object<UnaryExpression>(*this);
00448 }
00449 };
00450
00451
00452
00453
00454 class UnaryPlus
00455 : public UnaryExpression
00456 {
00457 public:
00458
00459
00460 UnaryPlus(ExpressionPtr expr)
00461 : UnaryExpression(3, "+", expr)
00462 {
00463
00464
00465 }
00466
00467
00468 UnaryPlus(const UnaryPlus& old)
00469 : UnaryExpression(old)
00470 {}
00471
00472
00473 ~UnaryPlus(void) {}
00474
00475 protected:
00476
00477 void p_accept(ExpressionVisitor& e)
00478 {
00479 e.visit(*this);
00480 }
00481
00482
00483 UnaryPlus(void)
00484 : UnaryExpression()
00485 {}
00486
00487 private:
00488
00489 friend class boost::serialization::access;
00490
00491 template<class Archive>
00492 void serialize(Archive &ar, const unsigned int)
00493 {
00494 ar & boost::serialization::base_object<UnaryExpression>(*this);
00495 }
00496 };
00497
00498
00499
00500
00501 class BinaryExpression
00502 : public Expression
00503 {
00504 public:
00505
00506
00507 BinaryExpression(const int& prec, const std::string& op,
00508 ExpressionPtr lhs, ExpressionPtr rhs)
00509 : Expression(prec), p_operator(op), p_LHS(lhs), p_RHS(rhs)
00510 {
00511 }
00512
00513
00514 BinaryExpression(const BinaryExpression& old)
00515 : Expression(old),
00516 p_operator(old.p_operator),
00517 p_LHS(old.p_LHS), p_RHS(old.p_RHS)
00518 {
00519 }
00520
00521
00522 ~BinaryExpression(void)
00523 {}
00524
00525 const std::string& op(void) const
00526 {
00527 return p_operator;
00528 }
00529
00530
00531 ExpressionPtr lhs(void)
00532 {
00533 return p_LHS;
00534 }
00535
00536
00537
00538 ExpressionPtr rhs()
00539 {
00540 return p_RHS;
00541 }
00542
00543 protected:
00544
00545
00546 const std::string p_operator;
00547
00548
00549 bool p_emptyLHS;
00550
00551
00552 ExpressionPtr p_LHS;
00553
00554
00555 ExpressionPtr p_RHS;
00556
00557
00558 BinaryExpression(void)
00559 : Expression()
00560 {}
00561
00562 std::string p_render(void) const
00563 {
00564 std::string s("");
00565 if (p_LHS) {
00566 if (p_LHS->precedence() > this->precedence()) {
00567 s += "[" + p_LHS->render() + "]";
00568 } else {
00569 s += p_LHS->render();
00570 }
00571 } else {
00572 s += "(empty)";
00573 }
00574 s += " " + this->p_operator + " ";
00575 if (p_RHS) {
00576 if (p_RHS->precedence() > this->precedence()) {
00577 s += "[" + p_RHS->render() + "]";
00578 } else {
00579 s += p_RHS->render();
00580 }
00581 } else {
00582 s += "(empty)";
00583 }
00584 return s;
00585 }
00586
00587 bool p_null(void) const
00588 {
00589 bool result(true);
00590 if (p_LHS && p_RHS) {
00591 result = p_operator.size() == 0 ||
00592 p_LHS->null() ||
00593 p_RHS->null();
00594 }
00595 return result;
00596 }
00597
00598 void p_accept(ExpressionVisitor& e)
00599 {
00600 e.visit(*this);
00601 }
00602
00603 private:
00604
00605 friend class boost::serialization::access;
00606
00607 template<class Archive>
00608 void serialize(Archive &ar, const unsigned int)
00609 {
00610 ar & boost::serialization::base_object<Expression>(*this);
00611 ar & const_cast<std::string&>(p_operator) & p_LHS & p_RHS;
00612 }
00613 };
00614
00615
00616
00617
00618 class Multiplication
00619 : public BinaryExpression
00620 {
00621 public:
00622
00623
00624 Multiplication(ExpressionPtr lhs, ExpressionPtr rhs)
00625 : BinaryExpression(5, "*", lhs, rhs)
00626 {
00627
00628
00629
00630
00631 }
00632
00633
00634 Multiplication(const Multiplication& old)
00635 : BinaryExpression(old)
00636 {}
00637
00638
00639 ~Multiplication(void)
00640 {}
00641
00642 protected:
00643
00644 void p_accept(ExpressionVisitor& e)
00645 {
00646 e.visit(*this);
00647 }
00648
00649
00650 Multiplication(void)
00651 : BinaryExpression()
00652 {}
00653
00654 private:
00655
00656 friend class boost::serialization::access;
00657
00658 template<class Archive>
00659 void serialize(Archive &ar, const unsigned int)
00660 {
00661 ar & boost::serialization::base_object<BinaryExpression>(*this);
00662 }
00663
00664 };
00665
00666
00667
00668
00669 inline
00670 ExpressionPtr operator*(ExpressionPtr lhs, ExpressionPtr rhs)
00671 {
00672 ExpressionPtr result(new Multiplication(lhs, rhs));
00673 return result;
00674 }
00675
00676 template <typename T>
00677 ExpressionPtr operator*(T lhs, ExpressionPtr rhs)
00678 {
00679 ExpressionPtr c(new ConstantExpression<T>(lhs));
00680 return c * rhs;
00681 }
00682
00683 template <typename T>
00684 ExpressionPtr operator*(ExpressionPtr lhs, T rhs)
00685 {
00686 ExpressionPtr c(new ConstantExpression<T>(rhs));
00687 return lhs * c;
00688 }
00689
00690 inline
00691 ExpressionPtr operator*(VariablePtr lhs, ExpressionPtr rhs)
00692 {
00693 ExpressionPtr v(new VariableExpression(lhs));
00694 return v * rhs;
00695 }
00696
00697 inline
00698 ExpressionPtr operator*(ExpressionPtr lhs, VariablePtr rhs)
00699 {
00700 ExpressionPtr v(new VariableExpression(rhs));
00701 return lhs * v;
00702 }
00703
00704 inline
00705 ExpressionPtr operator*(VariablePtr lhs, VariablePtr rhs)
00706 {
00707 ExpressionPtr lv(new VariableExpression(lhs));
00708 ExpressionPtr rv(new VariableExpression(rhs));
00709 return lv * rv;
00710 }
00711
00712 template <typename T>
00713 ExpressionPtr operator*(T lhs, VariablePtr rhs)
00714 {
00715 ExpressionPtr c(new ConstantExpression<T>(lhs));
00716 ExpressionPtr v(new VariableExpression(rhs));
00717 return c * v;
00718 }
00719
00720 template <typename T>
00721 ExpressionPtr operator*(VariablePtr lhs, T rhs)
00722 {
00723 ExpressionPtr v(new VariableExpression(lhs));
00724 ExpressionPtr c(new ConstantExpression<T>(rhs));
00725 return v * c;
00726 }
00727
00728
00729
00730
00731 inline
00732 ExpressionPtr operator*=(ExpressionPtr& lhs, ExpressionPtr rhs)
00733 {
00734 lhs = lhs * rhs;
00735 return lhs;
00736 }
00737
00738 template <typename T>
00739 ExpressionPtr operator*=(ExpressionPtr& lhs, T rhs)
00740 {
00741 ExpressionPtr c(new ConstantExpression<T>(rhs));
00742 lhs = lhs * c;
00743 return lhs;
00744 }
00745
00746 inline
00747 ExpressionPtr operator*=(ExpressionPtr& lhs, VariablePtr rhs)
00748 {
00749 ExpressionPtr v(new VariableExpression(rhs));
00750 lhs = lhs * v;
00751 return lhs;
00752 }
00753
00754
00755
00756
00757 class Division
00758 : public BinaryExpression
00759 {
00760 public:
00761
00762
00763 Division(ExpressionPtr lhs, ExpressionPtr rhs)
00764 : BinaryExpression(5, "/", lhs, rhs)
00765 {
00766
00767
00768
00769
00770 }
00771
00772
00773 ~Division(void)
00774 {}
00775
00776 protected:
00777
00778 void p_accept(ExpressionVisitor& e)
00779 {
00780 e.visit(*this);
00781 }
00782
00783
00784 Division(void)
00785 : BinaryExpression()
00786 {}
00787
00788 private:
00789
00790 friend class boost::serialization::access;
00791
00792 template<class Archive>
00793 void serialize(Archive &ar, const unsigned int)
00794 {
00795 ar & boost::serialization::base_object<BinaryExpression>(*this);
00796 }
00797 };
00798
00799
00800
00801
00802
00803 inline
00804 ExpressionPtr operator/(ExpressionPtr lhs, ExpressionPtr rhs)
00805 {
00806 ExpressionPtr result(new Division(lhs, rhs));
00807 return result;
00808 }
00809
00810 template <typename T>
00811 ExpressionPtr operator/(T lhs, ExpressionPtr rhs)
00812 {
00813 ExpressionPtr c(new ConstantExpression<T>(lhs));
00814 return c / rhs;
00815 }
00816
00817 template <typename T>
00818 ExpressionPtr operator/(ExpressionPtr lhs, T rhs)
00819 {
00820 ExpressionPtr c(new ConstantExpression<T>(rhs));
00821 return lhs / c;
00822 }
00823
00824 inline
00825 ExpressionPtr operator/(VariablePtr lhs, ExpressionPtr rhs)
00826 {
00827 ExpressionPtr v(new VariableExpression(lhs));
00828 return v / rhs;
00829 }
00830
00831 inline
00832 ExpressionPtr operator/(ExpressionPtr lhs, VariablePtr rhs)
00833 {
00834 ExpressionPtr v(new VariableExpression(rhs));
00835 return lhs / v;
00836 }
00837
00838 inline
00839 ExpressionPtr operator/(VariablePtr lhs, VariablePtr rhs)
00840 {
00841 ExpressionPtr lv(new VariableExpression(lhs));
00842 ExpressionPtr rv(new VariableExpression(rhs));
00843 return lv / rv;
00844 }
00845
00846
00847
00848 template <typename T>
00849 ExpressionPtr operator/(T lhs, VariablePtr rhs)
00850 {
00851 ExpressionPtr c(new ConstantExpression<T>(lhs));
00852 ExpressionPtr v(new VariableExpression(rhs));
00853 return c / v;
00854 }
00855
00856 template <typename T>
00857 ExpressionPtr operator/(VariablePtr lhs, T rhs)
00858 {
00859 ExpressionPtr v(new VariableExpression(lhs));
00860 ExpressionPtr c(new ConstantExpression<T>(rhs));
00861 return v / c;
00862 }
00863
00864
00865
00866
00867 inline
00868 ExpressionPtr operator/=(ExpressionPtr& lhs, ExpressionPtr rhs)
00869 {
00870
00871 lhs = lhs / rhs;
00872 return lhs;
00873 }
00874
00875 template <typename T>
00876 ExpressionPtr operator/=(ExpressionPtr& lhs, T rhs)
00877 {
00878 ExpressionPtr c(new ConstantExpression<T>(rhs));
00879
00880 lhs = lhs / c;
00881 return lhs;
00882 }
00883
00884 inline
00885 ExpressionPtr operator/=(ExpressionPtr& lhs, VariablePtr rhs)
00886 {
00887 ExpressionPtr v(new VariableExpression(rhs));
00888
00889 lhs = lhs / v;
00890 return lhs;
00891 }
00892
00893
00894
00895
00896
00897
00898 class Addition
00899 : public BinaryExpression
00900 {
00901 public:
00902
00903
00904 Addition(ExpressionPtr lhs, ExpressionPtr rhs)
00905 : BinaryExpression(6, "+", lhs, rhs)
00906 {
00907
00908
00909
00910
00911 }
00912
00913
00914 Addition(const Addition& old)
00915 : BinaryExpression(old)
00916 {}
00917
00918
00919 ~Addition(void)
00920 {}
00921
00922 protected:
00923
00924 void p_accept(ExpressionVisitor& e)
00925 {
00926 e.visit(*this);
00927 }
00928
00929
00930 Addition(void)
00931 : BinaryExpression()
00932 {}
00933
00934 private:
00935
00936 friend class boost::serialization::access;
00937
00938 template<class Archive>
00939 void serialize(Archive &ar, const unsigned int)
00940 {
00941 ar & boost::serialization::base_object<BinaryExpression>(*this);
00942 }
00943
00944 };
00945
00946
00947
00948
00949 class Subtraction
00950 : public BinaryExpression
00951 {
00952 public:
00953
00954
00955 Subtraction(ExpressionPtr lhs, ExpressionPtr rhs)
00956 : BinaryExpression(6, "-", lhs, rhs)
00957 {
00958
00959
00960
00961
00962 }
00963
00964
00965 Subtraction(const Subtraction& old)
00966 : BinaryExpression(old)
00967 {}
00968
00969
00970 ~Subtraction(void)
00971 {}
00972
00973 protected:
00974
00975 void p_accept(ExpressionVisitor& e)
00976 {
00977 e.visit(*this);
00978 }
00979
00980
00981 Subtraction(void)
00982 : BinaryExpression()
00983 {}
00984
00985 private:
00986
00987 friend class boost::serialization::access;
00988
00989 template<class Archive>
00990 void serialize(Archive &ar, const unsigned int)
00991 {
00992 ar & boost::serialization::base_object<BinaryExpression>(*this);
00993 }
00994
00995 };
00996
00997
00998
00999
01000 inline
01001 ExpressionPtr operator+(ExpressionPtr expr)
01002 {
01003 return ExpressionPtr(new UnaryPlus(expr));
01004 }
01005
01006 inline
01007 ExpressionPtr operator+(VariablePtr var)
01008 {
01009 ExpressionPtr expr(new VariableExpression(var));
01010 return +expr;
01011 }
01012 inline
01013 ExpressionPtr operator+(ExpressionPtr lhs, ExpressionPtr rhs)
01014 {
01015 ExpressionPtr result(new Addition(lhs, rhs));
01016 return result;
01017 }
01018
01019 template <typename T>
01020 ExpressionPtr operator+(T lhs, ExpressionPtr rhs)
01021 {
01022 ExpressionPtr c(new ConstantExpression<T>(lhs));
01023 return c + rhs;
01024 }
01025
01026 template <typename T>
01027 ExpressionPtr operator+(ExpressionPtr lhs, T rhs)
01028 {
01029 ExpressionPtr c(new ConstantExpression<T>(rhs));
01030 return lhs + c;
01031 }
01032
01033 template <typename T>
01034 ExpressionPtr operator+(T lhs, VariablePtr rhs)
01035 {
01036 ExpressionPtr v(new VariableExpression(rhs));
01037 ExpressionPtr c(new ConstantExpression<T>(lhs));
01038 return c + v;
01039 }
01040
01041 template <typename T>
01042 ExpressionPtr operator+(VariablePtr lhs, T rhs)
01043 {
01044 ExpressionPtr v(new VariableExpression(lhs));
01045 ExpressionPtr c(new ConstantExpression<T>(rhs));
01046 return v + c;
01047 }
01048
01049 inline
01050 ExpressionPtr operator+(VariablePtr lhs, ExpressionPtr rhs)
01051 {
01052 ExpressionPtr v(new VariableExpression(lhs));
01053 return v + rhs;
01054 }
01055
01056 inline
01057 ExpressionPtr operator+(ExpressionPtr lhs, VariablePtr rhs)
01058 {
01059 ExpressionPtr v(new VariableExpression(rhs));
01060 return lhs + v;
01061 }
01062
01063 inline
01064 ExpressionPtr operator+(VariablePtr lhs, VariablePtr rhs)
01065 {
01066 ExpressionPtr lv(new VariableExpression(lhs));
01067 ExpressionPtr rv(new VariableExpression(rhs));
01068 return lv + rv;
01069 }
01070
01071
01072
01073
01074 inline
01075 ExpressionPtr operator+=(ExpressionPtr& lhs, ExpressionPtr rhs)
01076 {
01077 if (lhs) {
01078 lhs = lhs + rhs;
01079 } else {
01080 lhs = rhs;
01081 }
01082 return lhs;
01083 }
01084
01085 template <typename T>
01086 ExpressionPtr operator+=(ExpressionPtr& lhs, T rhs)
01087 {
01088 ExpressionPtr c(new ConstantExpression<T>(rhs));
01089 return lhs += c;
01090 }
01091
01092 inline
01093 ExpressionPtr operator+=(ExpressionPtr& lhs, VariablePtr rhs)
01094 {
01095 ExpressionPtr v(new VariableExpression(rhs));
01096 return lhs += v;
01097 }
01098
01099
01100
01101
01102 inline
01103 ExpressionPtr operator-(ExpressionPtr expr)
01104 {
01105 return ExpressionPtr(new UnaryMinus(expr));
01106 }
01107
01108 inline
01109 ExpressionPtr operator-(VariablePtr var)
01110 {
01111 ExpressionPtr expr(new VariableExpression(var));
01112 return -expr;
01113 }
01114
01115 inline
01116 ExpressionPtr operator-(ExpressionPtr lhs, ExpressionPtr rhs)
01117 {
01118 ExpressionPtr result(new Subtraction(lhs, rhs));
01119 return result;
01120 }
01121
01122 template <typename T>
01123 ExpressionPtr operator-(T lhs, ExpressionPtr rhs)
01124 {
01125 ExpressionPtr c(new ConstantExpression<T>(lhs));
01126 return c - rhs;
01127 }
01128
01129 template <typename T>
01130 ExpressionPtr operator-(ExpressionPtr lhs, T rhs)
01131 {
01132 ExpressionPtr c(new ConstantExpression<T>(rhs));
01133 return lhs - c;
01134 }
01135
01136 template <typename T>
01137 ExpressionPtr operator-(T lhs, VariablePtr rhs)
01138 {
01139 ExpressionPtr v(new VariableExpression(rhs));
01140 ExpressionPtr c(new ConstantExpression<T>(lhs));
01141 return c - v;
01142 }
01143
01144 template <typename T>
01145 ExpressionPtr operator-(VariablePtr lhs, T rhs)
01146 {
01147 ExpressionPtr v(new VariableExpression(lhs));
01148 ExpressionPtr c(new ConstantExpression<T>(rhs));
01149 return v - c;
01150 }
01151
01152 inline
01153 ExpressionPtr operator-(VariablePtr lhs, ExpressionPtr rhs)
01154 {
01155 ExpressionPtr v(new VariableExpression(lhs));
01156 return v - rhs;
01157 }
01158
01159 inline
01160 ExpressionPtr operator-(ExpressionPtr lhs, VariablePtr rhs)
01161 {
01162 ExpressionPtr v(new VariableExpression(rhs));
01163 return lhs - v;
01164 }
01165
01166 inline
01167 ExpressionPtr operator-(VariablePtr lhs, VariablePtr rhs)
01168 {
01169 ExpressionPtr lv(new VariableExpression(lhs));
01170 ExpressionPtr rv(new VariableExpression(rhs));
01171 return lv - rv;
01172 }
01173
01174
01175
01176
01177 inline
01178 ExpressionPtr operator-=(ExpressionPtr& lhs, ExpressionPtr rhs)
01179 {
01180 if (lhs) {
01181 lhs = lhs - rhs;
01182 } else {
01183 lhs = - rhs;
01184 }
01185 return lhs;
01186 }
01187
01188 template <typename T>
01189 ExpressionPtr operator-=(ExpressionPtr& lhs, T rhs)
01190 {
01191 ExpressionPtr c(new ConstantExpression<T>(rhs));
01192 lhs -= c;
01193 return lhs;
01194 }
01195
01196 inline
01197 ExpressionPtr operator-=(ExpressionPtr& lhs, VariablePtr rhs)
01198 {
01199 ExpressionPtr v(new VariableExpression(rhs));
01200 lhs -= v;
01201 return lhs;
01202 }
01203
01204
01205
01206
01207 class Exponentiation
01208 : public BinaryExpression
01209 {
01210 protected:
01211
01212 public:
01213
01214
01215 Exponentiation(ExpressionPtr lhs, int exp)
01216 : BinaryExpression(2, "^", lhs, ExpressionPtr(new IntegerConstant(exp)))
01217 {
01218
01219
01220 }
01221
01222
01223 Exponentiation(const Exponentiation& old)
01224 : BinaryExpression(old)
01225 {}
01226
01227
01228 ~Exponentiation(void)
01229 {}
01230
01231 protected:
01232
01233 void p_accept(ExpressionVisitor& e)
01234 {
01235 e.visit(*this);
01236 }
01237
01238
01239 Exponentiation(void)
01240 : BinaryExpression()
01241 {}
01242
01243 private:
01244
01245 friend class boost::serialization::access;
01246
01247 template<class Archive>
01248 void serialize(Archive &ar, const unsigned int)
01249 {
01250 ar & boost::serialization::base_object<BinaryExpression>(*this);
01251 }
01252 };
01253
01254
01255
01256
01257
01258 inline
01259 ExpressionPtr operator^(ExpressionPtr expr, int exp)
01260 {
01261 ExpressionPtr result(new Exponentiation(expr, exp));
01262 return result;
01263 }
01264
01265 inline
01266 ExpressionPtr operator^(VariablePtr v, int exp)
01267 {
01268 ExpressionPtr vexpr(new VariableExpression(v));
01269 ExpressionPtr result(new Exponentiation(vexpr, exp));
01270 return result;
01271 }
01272
01273
01274
01275
01276
01277 class Constraint
01278 : public BinaryExpression,
01279 public utility::Named
01280 {
01281 public:
01282
01283
01284 Constraint(const int& prec, const std::string& op,
01285 ExpressionPtr lhs, ExpressionPtr rhs);
01286
01287
01288 ~Constraint(void)
01289 {}
01290
01291
01292 void addToLHS(ExpressionPtr e)
01293 {
01294 p_LHS += e;
01295 }
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331 void accept(ExpressionVisitor& visitor)
01332 {
01333 BinaryExpression::accept(visitor);
01334 }
01335
01336 protected:
01337
01338 static int p_nextID;
01339
01340
01341 Constraint(void);
01342
01343 private:
01344
01345 friend class boost::serialization::access;
01346
01347 template<class Archive>
01348 void serialize(Archive &ar, const unsigned int)
01349 {
01350 ar & boost::serialization::base_object<BinaryExpression>(*this);
01351 ar & boost::serialization::base_object<utility::Named>(*this);
01352 }
01353 };
01354
01355
01356
01357
01358 class LessThan
01359 : public Constraint
01360 {
01361 public:
01362
01363
01364 LessThan(ExpressionPtr lhs, ExpressionPtr rhs)
01365 : Constraint(8, "<", lhs, rhs)
01366 {}
01367
01368
01369 ~LessThan(void)
01370 {}
01371
01372 protected:
01373
01374 void p_accept(ExpressionVisitor& e)
01375 {
01376 e.visit(*this);
01377 }
01378
01379
01380 LessThan(void)
01381 : Constraint()
01382 {}
01383
01384 private:
01385
01386 friend class boost::serialization::access;
01387
01388 template<class Archive>
01389 void serialize(Archive &ar, const unsigned int)
01390 {
01391 ar & boost::serialization::base_object<Constraint>(*this);
01392 }
01393 };
01394
01395
01396
01397
01398 class LessThanOrEqual
01399 : public Constraint
01400 {
01401 public:
01402
01403
01404 LessThanOrEqual(ExpressionPtr lhs, ExpressionPtr rhs)
01405 : Constraint(8, "<=", lhs, rhs)
01406 {}
01407
01408
01409 ~LessThanOrEqual(void)
01410 {}
01411
01412 protected:
01413
01414 void p_accept(ExpressionVisitor& e)
01415 {
01416 e.visit(*this);
01417 }
01418
01419
01420 LessThanOrEqual(void)
01421 : Constraint()
01422 {}
01423
01424 private:
01425
01426 friend class boost::serialization::access;
01427
01428 template<class Archive>
01429 void serialize(Archive &ar, const unsigned int)
01430 {
01431 ar & boost::serialization::base_object<Constraint>(*this);
01432 }
01433 };
01434
01435
01436
01437
01438
01439 class GreaterThan
01440 : public Constraint
01441 {
01442 public:
01443
01444
01445 GreaterThan(ExpressionPtr lhs, ExpressionPtr rhs)
01446 : Constraint(8, ">", lhs, rhs)
01447 {}
01448
01449
01450 ~GreaterThan(void)
01451 {}
01452
01453 protected:
01454
01455 void p_accept(ExpressionVisitor& e)
01456 {
01457 e.visit(*this);
01458 }
01459
01460
01461 GreaterThan(void)
01462 : Constraint()
01463 {}
01464
01465 private:
01466
01467 friend class boost::serialization::access;
01468
01469 template<class Archive>
01470 void serialize(Archive &ar, const unsigned int)
01471 {
01472 ar & boost::serialization::base_object<Constraint>(*this);
01473 }
01474 };
01475
01476
01477
01478
01479 class GreaterThanOrEqual
01480 : public Constraint
01481 {
01482 public:
01483
01484
01485 GreaterThanOrEqual(ExpressionPtr lhs, ExpressionPtr rhs)
01486 : Constraint(8, ">=", lhs, rhs)
01487 {}
01488
01489
01490 ~GreaterThanOrEqual(void)
01491 {}
01492
01493 protected:
01494
01495 void p_accept(ExpressionVisitor& e)
01496 {
01497 e.visit(*this);
01498 }
01499
01500
01501 GreaterThanOrEqual(void)
01502 : Constraint()
01503 {}
01504
01505 private:
01506
01507 friend class boost::serialization::access;
01508
01509 template<class Archive>
01510 void serialize(Archive &ar, const unsigned int)
01511 {
01512 ar & boost::serialization::base_object<Constraint>(*this);
01513 }
01514 };
01515
01516
01517
01518
01519 class Equal
01520 : public Constraint
01521 {
01522 public:
01523
01524
01525 Equal(ExpressionPtr lhs, ExpressionPtr rhs)
01526 : Constraint(9, "=", lhs, rhs)
01527 {}
01528
01529
01530 ~Equal(void)
01531 {}
01532
01533 protected:
01534
01535 void p_accept(ExpressionVisitor& e)
01536 {
01537 e.visit(*this);
01538 }
01539
01540
01541 Equal(void)
01542 : Constraint()
01543 {}
01544
01545 private:
01546
01547 friend class boost::serialization::access;
01548
01549 template<class Archive>
01550 void serialize(Archive &ar, const unsigned int)
01551 {
01552 ar & boost::serialization::base_object<Constraint>(*this);
01553 }
01554 };
01555
01556
01557
01558 typedef boost::shared_ptr<Constraint> ConstraintPtr;
01559
01560
01561
01562
01563
01564 template <typename T>
01565 ConstraintPtr operator<(ExpressionPtr lhs, T rhs)
01566 {
01567 ExpressionPtr c(new ConstantExpression<T>(rhs));
01568 ConstraintPtr result(new LessThan(lhs, c));
01569 return result;
01570 }
01571
01572 template <typename T>
01573 ConstraintPtr operator<(VariablePtr lhs, T rhs)
01574 {
01575 ExpressionPtr v(new VariableExpression(lhs));
01576 ExpressionPtr c(new ConstantExpression<T>(rhs));
01577 ConstraintPtr result(new LessThan(v, c));
01578 return result;
01579 }
01580
01581
01582
01583
01584 template <typename T>
01585 ConstraintPtr operator<=(ExpressionPtr lhs, T rhs)
01586 {
01587 ExpressionPtr c(new ConstantExpression<T>(rhs));
01588 ConstraintPtr result(new LessThanOrEqual(lhs, c));
01589 return result;
01590 }
01591
01592 template <typename T>
01593 ConstraintPtr operator<=(VariablePtr lhs, T rhs)
01594 {
01595 ExpressionPtr v(new VariableExpression(lhs));
01596 ExpressionPtr c(new ConstantExpression<T>(rhs));
01597 ConstraintPtr result(new LessThanOrEqual(v, c));
01598 return result;
01599 }
01600
01601
01602
01603
01604 template <typename T>
01605 ConstraintPtr operator>(ExpressionPtr lhs, T rhs)
01606 {
01607 ExpressionPtr c(new ConstantExpression<T>(rhs));
01608 ConstraintPtr result(new GreaterThan(lhs, c));
01609 return result;
01610 }
01611
01612 template <typename T>
01613 ConstraintPtr operator>(VariablePtr lhs, T rhs)
01614 {
01615 ExpressionPtr v(new VariableExpression(lhs));
01616 ExpressionPtr c(new ConstantExpression<T>(rhs));
01617 ConstraintPtr result(new GreaterThan(v, c));
01618 return result;
01619 }
01620
01621
01622
01623 template <typename T>
01624 ConstraintPtr operator>=(ExpressionPtr lhs, T rhs)
01625 {
01626 ExpressionPtr c(new ConstantExpression<T>(rhs));
01627 ConstraintPtr result(new GreaterThanOrEqual(lhs, c));
01628 return result;
01629 }
01630
01631 template <typename T>
01632 ConstraintPtr operator>=(VariablePtr lhs, T rhs)
01633 {
01634 ExpressionPtr v(new VariableExpression(lhs));
01635 ExpressionPtr c(new ConstantExpression<T>(rhs));
01636 ConstraintPtr result(new GreaterThanOrEqual(v, c));
01637 return result;
01638 }
01639
01640
01641
01642
01643 template <typename T>
01644 ConstraintPtr operator==(ExpressionPtr lhs, T rhs)
01645 {
01646 ExpressionPtr c(new ConstantExpression<T>(rhs));
01647 ConstraintPtr result(new Equal(lhs, c));
01648 return result;
01649 }
01650
01651 template <typename T>
01652 ConstraintPtr operator==(VariablePtr lhs, T rhs)
01653 {
01654 ExpressionPtr v(new VariableExpression(lhs));
01655 ExpressionPtr c(new ConstantExpression<T>(rhs));
01656 ConstraintPtr result(new Equal(v, c));
01657 return result;
01658 }
01659
01660
01661
01662
01663 class ExpressionChecker
01664 : public ExpressionVisitor
01665 {
01666 public:
01667
01668
01669 bool isConstant;
01670
01671
01672 bool isInteger;
01673
01674
01675 bool isVariable;
01676
01677
01678 bool isExponentiation;
01679
01680
01681 ExpressionChecker(void)
01682 : ExpressionVisitor(),
01683 isConstant(false), isInteger(false), isVariable(false), isExponentiation(false)
01684 {}
01685
01686
01687 ~ExpressionChecker(void)
01688 {}
01689
01690 void visit(IntegerConstant& e)
01691 {
01692 isConstant = true;
01693 isInteger = true;
01694 }
01695 void visit(RealConstant& e)
01696 {
01697 isConstant = true;
01698 }
01699 void visit(VariableExpression& e)
01700 {
01701 isVariable = true;
01702 }
01703 void visit(Exponentiation& e)
01704 {
01705 isExponentiation = true;
01706 }
01707 };
01708
01709 }
01710 }
01711
01712 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::ConstantExpression<int>)
01713 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::ConstantExpression<double>)
01714
01715 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::VariableExpression)
01716
01717 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::UnaryMinus)
01718 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::UnaryPlus)
01719
01720 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::Multiplication)
01721 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::Division)
01722 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::Addition)
01723 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::Subtraction)
01724 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::Exponentiation)
01725
01726 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::Constraint)
01727 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::LessThan)
01728 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::LessThanOrEqual)
01729 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::GreaterThan)
01730 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::GreaterThanOrEqual)
01731 BOOST_CLASS_EXPORT_KEY(gridpack::optimization::Equal)
01732
01733
01734 #endif